package com.yun.controller.loginRegister;

import com.yun.pojo.User;
import com.yun.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.HashMap;
import java.util.Map;

@Controller
public class RegisterController {

    @Autowired
    UserService userService;

    @RequestMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    @PostMapping("/register")
    public String register(@RequestParam("username") String name,
                           @RequestParam("password") String pwd,
                           @RequestParam("phone") String phone,
                           @RequestParam("email") String email,
                           @RequestParam("sex") int sex,
                           Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (phone.length()==11) {
            if(email.length()>6){
            String end = email.substring(email.length() - 4);
                  if (!end.equals(".com")) {
                     model.addAttribute("msg", "请输入正确邮箱格式");
                     return "register";
                  }
                  map.put("phone", phone);
                  map.put("name", name);
                  map.put("pwd", pwd);
                  map.put("email", email);
                  map.put("sex", sex);
                  userService.register(map);
                  return "login";
            }
             model.addAttribute("msg", "请输入正确邮箱格式");
              return "register";
        }
        model.addAttribute("error", "请输入11位手机号码");
        return "register";
    }

}